package com.reporting.yb.module.system.service.wxusers;

import com.reporting.yb.framework.common.pojo.PageResult;
import com.reporting.yb.framework.common.util.object.BeanUtils;
import com.reporting.yb.framework.common.util.wechatlogin.WeChatApiUtil;
import com.reporting.yb.module.system.controller.admin.wxusers.vo.WeChatLoginVO;
import com.reporting.yb.module.system.controller.admin.wxusers.vo.WxUsersPageReqVO;
import com.reporting.yb.module.system.controller.admin.wxusers.vo.WxUsersSaveReqVO;
import com.reporting.yb.module.system.dal.dataobject.wxusers.WeChatUserDO;
import com.reporting.yb.module.system.dal.dataobject.wxusers.WxUsersDO;
import com.reporting.yb.module.system.dal.mysql.wxusers.WxUserMapper;
import com.reporting.yb.module.system.dal.mysql.wxusers.WxUsersMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import static com.reporting.yb.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.reporting.yb.module.system.enums.ErrorCodeConstants.WX_USERS_NOT_EXISTS;


/**
 * 微信登录 用户 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class WxUsersServiceImpl implements WxUsersService {


    @Override
    public WeChatUserDO getByCode(WeChatLoginVO weChatLoginVO) {
        String code = weChatLoginVO.getCode();

        // 调用微信API获取openid和session_key
        WeChatApiUtil.WeChatLoginResult result = WeChatApiUtil.getOpenIdByCode(code);
        if (result == null || result.getOpenid() == null) {
            throw new RuntimeException("微信登录失败，无法获取openid");
        }

        // 查询数据库中是否存在该openid
        WeChatUserDO weChatUser = WxUsersMapper.selectByOpenid(result.getOpenid());
        if (weChatUser == null) {
            // 如果不存在，则插入数据库
            weChatUser = new WeChatUserDO();
            weChatUser.setOpenId(result.getOpenid());
//            wxUserMapper.insert(weChatUser);
        }

        return weChatUser;
    }

    @Resource
    private WxUsersMapper wxUsersMapper;

    @Resource
    private WxUserMapper wxUserMapper;

    @Override
    public String createWxUsers(WxUsersSaveReqVO createReqVO) {
        // 插入
        WxUsersDO wxUsers = BeanUtils.toBean(createReqVO, WxUsersDO.class);
        wxUsersMapper.insert(wxUsers);
        // 返回
        return wxUsers.getId();
    }

    @Override
    public void updateWxUsers(WxUsersSaveReqVO updateReqVO) {
        // 校验存在
        validateWxUsersExists(updateReqVO.getId());
        // 更新
        WxUsersDO updateObj = BeanUtils.toBean(updateReqVO, WxUsersDO.class);
        wxUsersMapper.updateById(updateObj);
    }

    @Override
    public void deleteWxUsers(String id) {
        // 校验存在
        validateWxUsersExists(id);
        // 删除
        wxUsersMapper.deleteById(id);
    }

    private void validateWxUsersExists(String id) {
        if (wxUsersMapper.selectById(id) == null) {
            throw exception(WX_USERS_NOT_EXISTS);
        }
    }

    @Override
    public WxUsersDO getWxUsers(String id) {
        return wxUsersMapper.selectById(id);
    }

    @Override
    public PageResult<WxUsersDO> getWxUsersPage(WxUsersPageReqVO pageReqVO) {
        return wxUsersMapper.selectPage(pageReqVO);
    }

}